//app.js
const Api = require('./config')
const http = require("./utils/http.js");
const moment = require("./utils/moment.min.js");
App({
  onLaunch: function () {
  },
  onShow(data) {
    console.log(data)
    this.globalData.userInfo = wx.getStorageSync('USER-BASE-INFO') || {}
    this.globalData.mainStoreId = wx.getStorageSync('CARD-STORE-ID') || ''
    if (data && data.referrerInfo) {
      let referrerInfo = JSON.parse(JSON.stringify(data.referrerInfo))
      if (referrerInfo.appId === 'wxeb490c6f9b154ef9') {
        this.globalData.referrerInfo = referrerInfo
      } else if (referrerInfo.appId === 'wx348ac60df4cdd37f') {
        this.globalData.mallInfo = referrerInfo
      }
      data.referrerInfo = null
    }
    console.log(this.globalData)
  },
  loginUser() {
    var that = this
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        that.code = res.code
        console.log(res)
        wx.getSetting({
          success: function (res) {
            console.log(res)
            if (res.authSetting['scope.userInfo']) {
              // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
              console.log('-------------')
              wx.getUserInfo({
                success: res => {
                  console.log(res)
                  // 可以将 res 发送给后台解码出 unionId
                  that.globalData.userInfo = res.userInfo
                  wx.setStorage({
                    key: "userInfo",
                    data: res.userInfo
                  })
                  // that.post(that.Apis.POST_WECHAT_LOGIN, {
                  //   encryptedData: res.encryptedData,
                  //   iv: res.iv,
                  //   code: that.code,
                  //   wechatId: that.wechatId
                  // }, result => {
                  //   console.log(result)
                  //   that.globalData.userdata = result.data
                  //   that.openId = result.data.openid
                  //   callBack(that.openId)
                  // })
                }
              })
            }
          },
          fail: res => {
            console.log(res)
          }
        })
      }
    })
  },
  Apis: Api,
  post: http.post,
  ver: http.ver,
  moment: moment,
  hasPhoneNum:false,
  wechatId:'carelinker_xcx_consult',
  userId:'',
  mainStoreId:'',
  consultDrugId:'4d1e56a10a8b4aedbcb939f7b6e0f07c',
  openid:'',
  drugList:[],
  branchStoreId:'',
  isConfigQuestion:false,//健康咨询是否配置
  isConfigDrug: false,//药品评价是否配置
  belongToId:'xcx_carelinker_consult',
  globalData: {
    wechatId: '',
    cardId: '',
    userInfo: null,
    mainStoreId: '',
    branchStoreId: '',
    unionId: '',
    openid: '',
    referrerInfo: null,
    mallInfo: null,
    config: {},
    xcxWechatId: 'carelinker_xcx_consult'
  },
  getPhoneNum(e,callBack){
    var that = this
    wx.login({
      success: function (res) {
        console.log(e,res)
        if (res.code) {
          if (e.detail.iv) {
            that.post(that.Apis.POST_WECHAT_INFO, {
              encryptedData: e.detail.encryptedData,
              iv: e.detail.iv,
              code: res.code,
              wechatId: that.wechatId
            }, result => {
              console.log(result)
              if (result.success) {
                that.hasPhoneNum = result.data.phoneNumber != null && result.data.phoneNumber != ''
                that.userId = result.data.userId
                that.openid = result.data.openid
                if (that.hasPhoneNum){
                  wx.setStorage({
                    key: "userinfo",
                    data: result.data
                  })
                }
                callBack(result)
              }
            })
          } else if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
            // 用户拒绝
          } else { //微信未绑定手机号
          }
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  getSysConfig(groupId,callBack){
    var that = this
    that.post(that.Apis.GET_CONFIGS, {
      groupId: groupId,
      belongToId: that.belongToId
    }, result => {
      console.log(result)
      if (result.success) {
        callBack(result)
      }
    })
  }
})